package com.cardvalue.sys.tool;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cardvalue.sys.activity.BindUserActivity;
import com.cardvalue.sys.R;

/**
 * Created by cardvalue on 2016/4/11.
 */
public class MessageBox {
    public static ImageView ivGetCode;

    /**
     * 简单的显示dialog
     * @param dialog
     * @param title
     * @param message
     */
    public static void show(ProgressDialog dialog, String title, String message){
        dialog.show();
        dialog.getWindow().setContentView(R.layout.dialog_show);
        TextView tv=(TextView)dialog.getWindow().findViewById(R.id.message);
        ImageView iv = (ImageView)dialog.getWindow().findViewById(R.id.progressBar1);
        AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
        ad.start();
        dialog.setCancelable(false); //判断dialog是否可以点击
        tv.setText(message);
    }

    /**
     * Toast框
     * @param content
     * @param context
     */
    public static void ToastShow(String content, Context context){
        Toast toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER,0,-50);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_show, null);
        ImageView iv=(ImageView)view.findViewById(R.id.progressBar1);
        iv.setVisibility(View.GONE);
        TextView tv = (TextView)view.findViewById(R.id.message);
        tv.setText(content);
        toast.setView(view);
        toast.show();
    }

    /**
     * 手机验证码
     * @param context
     * @param url
     * @param dialog1
     * @param phone
     * @param handler
     */
    public static void EditAlertReg(final Context context, final String url, final ProgressDialog dialog1, final String phone, final Handler handler){
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View view =LayoutInflater.from(context).inflate(R.layout.normal_reg,null);
        builder.setView(view);
        final Dialog dialog = builder.show();
        Button cancel_btn = (Button)view.findViewById(R.id.cancel_btn);
        Button confirm_btn = (Button)view.findViewById(R.id.confirm_btn);
        final EditText tv = (EditText) view.findViewById(R.id.message);
        ivGetCode=(ImageView)view.findViewById(R.id.iv_getCode);
        ivGetCode.setImageURI(Uri.parse(url));
        tv.setText(tv.getText().toString());

        //点击取消息按钮后的操作
        cancel_btn.setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BindUserActivity.isRefrensh=true;
                dialog.dismiss();
            }
        });


        //点击重新获取验证码
        ivGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BindUserActivity.isRefrensh=false;
            //重新获取验证码
             handler.sendEmptyMessage(0);
            }
        });

        //点击确定后的操作
        confirm_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tvmessage=tv.getText().toString().trim();
                 if(tvmessage.equals("")){
                     MessageBox.ToastShow("请输入验证码", context);
                     return;
                 }else if(tvmessage.length()<4){
                     MessageBox.ToastShow("请输入正确的验证码", context);
                     return;
                 }else if(!tvmessage.equals("")){
                    //发送短信验证码
                    Message ms=new Message();
                    Bundle bundle=new Bundle();
                    ms.what=1;
                    bundle.putString("imageVerifyCode",tvmessage);
                    ms.setData(bundle);
                    handler.sendMessage(ms);
                    dialog.dismiss();

                }/*else{
                    MessageBox.ToastShow("请输入验证码", context);
                    //没有输入验证码
                    //BindUserActivity.isRefrensh=false;
                   // handler.sendEmptyMessage(0);
                }*/
            }
        });
    }

    /**
     * dialog弹出来以后，点击再发验证码的时候
     * @param context
     * @param url
     */
    public static void setVerifyCode(Context context,String url){
        ivGetCode.setImageURI(Uri.parse(url));
    }

}
